<?php

/**
 * This is the model class for table "img".
 *
 * The followings are the available columns in table 'img':
 * @property string $id
 * @property string $path
 * @property string $name
 *
 * The followings are the available model relations:
 * @property DealImg[] $dealImgs
 */
class Img extends CActiveRecord
{
	public $_file;
	
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Img the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'img';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id', 'required'),
			array('id', 'length', 'max'=>10),
			array('path, name', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, path, name', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'dealImgs' => array(self::HAS_MANY, 'DealImg', 'img_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'path' => 'Path',
			'name' => 'Name',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('path',$this->path,true);
		$criteria->compare('name',$this->name,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 
	 */
	public function getUploadImgPath() {
		return Yii::getPathOfAlias('webroot.uploads.images') . '/'; 
	}
	
	/**
	 * 
	 * @param int id
	 */
	public static function getImgsByDealId($id) {
		$criteria = new CDbCriteria;
		$criteria->join = 'INNER JOIN deal_img AS di ON di.img_id =  t.id AND deal_id = '. $id;
		return Img::model()->findAll($criteria);
	}
	
	/**
	 * save image 
	 */
	public function loadImage() {
		if (is_object($this->_file)) {
			$pathPrefix = $this->getUploadImgPath();
			$fileName = $this->_file->getName();
			$fileFullName = $pathPrefix . $fileName;
			$fileName = Img::createFileName(); 
			$fielType = str_replace("image/", "", $this->_file->getType());
			$this->name = $fileName . '.' . $fielType;
			$this->path = $pathPrefix;
			$fileFullName = $pathPrefix . $fileName . '.' . $fielType;
			$this->_file->saveAs($fileFullName);
				
			if (file_exists($fileFullName)) {
				return true;
			}
		} else {
			return false;
		}
	}
	
	/**
	 * delete image
	 */
	public function deleteFile() {
		if(is_file($this->path . $this->name)) {
			if(unlink($this->path . $this->name)) 
				return true;					
		}	
	}
	
	/**
	 * 
	 */
	public static function createFileName() {
		$str = substr(md5(microtime()),0,10);
		return $str;
	}
}